// 路由守卫
import { Navigate, useLocation } from "react-router-dom";
import { matchRouter } from "@/untils/releasRouter";
import { routeItem } from "@/router";
import { getToken } from "@/untils/auth";

function AuthRouter(props: { children: JSX.Element }) {
    // const token = getToken();
    const token = "123456";

    const { pathname } = useLocation();
    // 有token就不再进入login页面 
    if (token && pathname == '/login') {
        return <Navigate to='/layout/home' />
    }
    // 放开逻辑不进行token拦截的地址
    const releasValue: any = matchRouter(pathname, routeItem)
    if (releasValue.meta?.releas) {
        return props.children;
    }

    if (!token) {
        return <Navigate to='/login' />
    } else {
        return props.children;
    }
}

export default AuthRouter;